﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using EasyBuy.IDAL;
using EasyBuy.Model;

namespace EasyBuy.DAL
{
    /// <summary>
    /// 用户表服务类
    /// By:田玉粮
    /// 2012-7-15
    /// </summary>
    //public class UserService : IUserService
    public class UserService
    {
        #region 成员
        /// <summary>
        /// SQL语句：用户登录
        /// </summary>
        String SELECT_Users_By_Name = "select uid,uname,upassword,ustatus from users where uname=@uname and upassword=@upassword";
        #endregion

        #region 方法

        /// <summary>
        /// 查找用户是否存在
        /// </summary>
        /// <param name="UName">用户名</param>
        /// <param name="UPassword">用户密码</param>
        /// <returns>返回查找到的用户对象</returns>
        public Model.User GetUserByNameOrPass(string UName, string UPassword)
        {
            User user = null;
            using (SqlConnection conn = EasyBuyUntil.GetEasyBuyConn)
            {
                using (SqlCommand cmd = new SqlCommand(SELECT_Users_By_Name, conn))
                {
                    conn.Open();
                    cmd.Parameters.AddWithValue("@uname", UName);
                    cmd.Parameters.AddWithValue("@upassword", UPassword);
                    using (SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
                    {
                        if (dr.Read())
                        {
                            user = new User();
                            user.Uid = dr.GetInt32(0);
                            user.UName = dr["uname"].ToString();
                            user.UPassword = dr["upassword"].ToString();
                            user.UStatus = dr["ustatus"].ToString();
                        } 
                    }
                }
            }
            return user;
        }
        #endregion


    }

}
